import { gameConfigHandle } from "core/config/gameConfig"
import utils from "core/utils/utils"
import { PlayerModel } from "core/model/playerModel"
import playerHandle from "./playerHandle"
import { PartnerModel } from "core/model/partnerModel"
import { TreasureItemModel } from "core/model/treasureModel"
import gameUtils from "core/utils/gameUtils"


class PartnerHandle {
    //判断进阶技能是否解锁
    starSkillIsUnlock = (partner: PartnerModel, index: number) => {
        let star = partner.starLvl
        let skillUnLockStarLvl = gameConfigHandle.gameConfig.partner.skillUnLockStarLvl[index]
        if (star < skillUnLockStarLvl) {
            return false
        }
        if (!partner.starSkillUnlock.includes(index)) {
            return false
        }
        return true
    }
    //判断是否有单排5个英雄
    isFiveInOneRow(player: PlayerModel) {
        let teamList = player.team.teamList
        let row1 = 0
        let row2 = 0
        teamList.forEach((id, i) => {
            if (id) {
                if (i > 4) {
                    row2++
                } else {
                    row1++
                }
            }
        })
        if (row1 >= 5 || row2 >= 5) {
            return true
        }
        return false
    }
    getPartnerByIndexID(player: PlayerModel, indexID: number) {
        for (let i = 0; i < player.team.list.length; i++) {
            let partner = player.team.list[i]
            if (partner && partner.indexID == indexID) {
                return partner
            }
        }
        return null
    }

    getAttriValues(attriName, lvl, attriCoes: number[]) {
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let attriLvlupConfig = gameConfigHandle.gameConfig.attriLvlup
        let values = attriLvlupConfig.attriValuesList[attriName]
        let _lvl = lvl * attriCoes[0]
        let valueCoe = attriCoes[1] || 1
        let addLvl = attriCoes[2] || 0
        if (lvl > 0) {
            _lvl += addLvl
        }
        let value = utils.getIncValues(values, _lvl) * valueCoe
        value = utils.floor(value, attriLvlupConfig.attriValuesList[attriName][4] || 0)
        return value
    }

    getAltarAttriValues(attriName, lvl, attriCoes: number[]) {
        let altarConfig = gameConfigHandle.gameConfig.altar
        let attriLvlupConfig = altarConfig.lvlupAttris
        let values = attriLvlupConfig[attriName]
        let _lvl = lvl * attriCoes[0]
        let valueCoe = attriCoes[1] || 1
        let addLvl = attriCoes[2] || 0
        if (lvl > 0) {
            _lvl += addLvl
        }
        let value = utils.getIncValues(values, _lvl) * valueCoe
        value = utils.floor(value, attriLvlupConfig[attriName][4] || 0)
        return value
    }

    getBaseAttirPoint = (player: PlayerModel) => {
        let partnerConfig = gameConfigHandle.gameConfig.partner
        let partner = playerHandle.getPartner(player)
        let totalPoint = Math.floor((partner.lvl - 1) * partnerConfig.lvlUpGetBaseAttriFreePoint)
        let usePoint = 0
        utils.keys(partnerConfig.baseAttri).forEach(attriName => {
            usePoint += partner.baseAttris[attriName]
        })
        usePoint -= (partner.lvl - 1) * partnerConfig.lvlUpGetBaseAttriPoint
        return { totalPoint, usePoint, remainPoint: totalPoint - usePoint }
    }

    getPartnerStarLvl = (lvl) => {
        let partnerConfig = gameConfigHandle.gameConfig.partner
        for (let i = 0; i < partnerConfig.partnerStarlvls.length; i++) {
            if (lvl <= partnerConfig.partnerStarlvls[i]) {
                return i
            }
        }
        return 0
    }

    getHeroTotalStar = (player: PlayerModel) => {
        let totalStar = 0
        utils.values(player.team.recruitList).forEach((partner: PartnerModel) => {
            if (partner) {
                totalStar += partner.starLvl
            }
        })
        return totalStar
    }

    getTreasureTotalStar = (player: PlayerModel) => {
        let totalStar = 0
        utils.values(player.treasure.list).forEach((item: TreasureItemModel) => {
            if (item) {
                totalStar += item.lvl
            }
        })
        return totalStar
    }

    altarAttriCanLvlup = (player:PlayerModel) => {
        let canLvlup = false
        let mapID = 300016
        if (!gameUtils.isMapLimit(player, mapID)) {
            let altar = player.altar
            for (let i = 0; i < gameConfigHandle.gameConfig.altar.attriTypeList.length; i++) {
                const attriItem = gameConfigHandle.gameConfig.altar.attriTypeList[i];
                let attriLvl = altar.attriLvls[i] || 0
                if (attriLvl < gameConfigHandle.gameConfig.altar.altarStrMaxLvl) {
                    let res = gameUtils.getNeedResource(player, gameConfigHandle.gameConfig.need.altarStrNeed[i], attriLvl + 1, 1).err
                    if (res == 0) {
                        canLvlup = true
                        break
                    }
                }
            }
        }
        return canLvlup
    }
}


let partnerHandle = new PartnerHandle()

export default partnerHandle
